﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CourseDataManage.cs" company="HelloWorld">
//   BY黄媛媛
// </copyright>
// <summary>
//   Defines the CourseDataManage type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WindowsFormsApp2.Service
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using WindowsFormsApp2.Model;

    /// <summary>
    /// The course data manage.
    /// </summary>
    internal class CourseDataManage
    {
        /// <summary>
        /// The courses.
        /// </summary>
        private readonly List<Course> courses = Course.Courses;

        /// <summary>
        /// The link.
        /// </summary>
        private readonly LinkDB link = new LinkDB();

        /// <summary>
        /// The data initialization.
        /// </summary>
        public void DataInit()
        {
            this.link.GetCourses();
        }

        /// <summary>
        /// The delete course.
        /// </summary>
        /// <param name="weekday">
        /// The weekday.
        /// </param>
        /// <param name="chooseLesson">
        /// The choose lesson.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool DeleteCourse(int weekday, int chooseLesson)
        {
            if (!this.courses.Exists(
                    c => c.Weekday == weekday && c.BeginLesson <= chooseLesson
                                              && (c.LastLesson + c.BeginLesson) > chooseLesson))
            {
                return false;
            } // 无课程
            var cId = this.courses.Find(
                c => c.Weekday == weekday && c.BeginLesson <= chooseLesson
                                          && (c.LastLesson + c.BeginLesson) > chooseLesson).CourseId.ToString();
            this.link.DeleteCourse(cId);
            new DDLManage().DeleteByCourse(cId); // 删除课程对应的ddl
            this.courses.RemoveAll(c => c.Weekday == weekday && c.BeginLesson <= chooseLesson && (c.LastLesson + c.BeginLesson) > chooseLesson);
            return true;
        }

        /// <summary>
        /// The get course info.
        /// </summary>
        /// <param name="weekday">
        /// The weekday.
        /// </param>
        /// <param name="chooseLesson">
        /// The choose lesson.
        /// </param>
        /// <param name="courseId">
        /// The course id.
        /// </param>
        /// <param name="courseName">
        /// The course name.
        /// </param>
        public void GetCourseInfo(int weekday, int chooseLesson, out string courseId, out string courseName)
        {
            courseId = string.Empty;
            courseName = string.Empty;
            var query = from c in this.courses
                        where c.Weekday == weekday && c.BeginLesson <= chooseLesson && (c.LastLesson + c.BeginLesson) > chooseLesson
                        select c;
            try
            {
                var enumerable = query as Course[] ?? query.ToArray();
                courseId = enumerable.ElementAt(0).CourseId.ToString();
                courseName = enumerable.ElementAt(0).Name;
            }
            catch (Exception)
            {
                // ignored
            }
        }

        /// <summary>
        /// The add course.
        /// </summary>
        /// <param name="courseId">
        /// The course id.
        /// </param>
        /// <param name="courseName">
        /// The course name.
        /// </param>
        /// <param name="weekday">
        /// The weekday.
        /// </param>
        /// <param name="beginLesson">
        /// The begin lesson.
        /// </param>
        /// <param name="lastLesson">
        /// The last lesson.
        /// </param>
        public void AddCourse(long courseId, string courseName, int weekday, int beginLesson, int lastLesson)
        {
            var newCourse = new Course(courseId, courseName, weekday, beginLesson, lastLesson);
            this.courses.Add(newCourse);
            this.link.AddCourse(newCourse);
        }
    }
}
